package com.android.lovely.ui.dialog

import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.android.lovely.data.TradeType
import com.android.lovely.data.TransactionListBean
import com.android.lovely.data.tradeTypeMap
import com.android.lovely.databinding.DialogTransTypeBinding
import com.android.lovely.databinding.ItemTransTypeBinding
import com.android.lovely.framework.BaseAdapter
import com.android.lovely.framework.BaseDialogFragment
import com.android.lovely.framework.BaseLoadAdapter
import com.android.lovely.ui.viewmodel.TransViewModel
import com.kotlin.extensions.view.ViewExt.clickable

/**
 *
 * @author why
 * @date 2025/3/13 18:55
 */
class TransTypeDialog : BaseDialogFragment<DialogTransTypeBinding>() {
    private var type: TradeType? = TradeType.getTypeById(null)
    val viewModel by activityViewModels<TransViewModel>()

    val transAdapter by lazy {
        TansTypeAdapter()
    }

    override fun initializeViews() {
        type = viewModel.type
        binding.ivClose.clickable {
            dismissAllowingStateLoss()
        }
        binding.rv.apply {
            layoutManager = LinearLayoutManager(requireContext())
            adapter = transAdapter
            transAdapter.updateType(type)
            transAdapter.onItemClick = { item, _ ->
                type = item
                transAdapter.updateType(type)
                adapter?.notifyDataSetChanged()
            }
            transAdapter.updateList(TradeType.getAll())
        }
        binding.confirmBtn.clickable {
            viewModel.type = type
            viewModel.updateType()
            dismissAllowingStateLoss()
        }
    }

    override fun isBottom(): Boolean {
        return true
    }

}

class TansTypeAdapter() : BaseAdapter<TradeType, ItemTransTypeBinding>() {
    var tradeType: TradeType? = null
    fun updateType(type: TradeType?) {
        tradeType = type
    }
    override fun bindViewWithHolderDetails(
        holder: ViewHolder,
        item: TradeType?,
        binding: ItemTransTypeBinding,
        position: Int
    ) {
        item ?: return
        binding.apply {
            text.setText(item.contentResId)
            text.isSelected = item.typeId == tradeType?.typeId
        }
    }

}